﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SecurityEvaluate;
using SmartWeb.BLL;
using Yipusoft.Account.Model;
public partial class Diary_DiaryDisplay : System.Web.UI.Page
{
    DiaryDataContext db = new DiaryDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        Guid id = new Guid(Request["id"]);
        BindDiary(id);
        BindDuty(id);
        bindInspect(id);
        BindFamily(id);
        bindLeave(id);

    }

    void BindDiary(Guid id)
    {
        DiarySquadron diary = db.DiarySquadron.Where<DiarySquadron>(o => o.Id == id).FirstOrDefault();
        lblDiaryDate.Text = diary.DiaryDate.Value.ToString("yyyy年MM月dd日") + "&nbsp;&nbsp;" + UserFun.GetWeek(((DayOfWeek)diary.DiaryDate.Value.DayOfWeek));
        Weather.Text = diary.Weather;
        Watch.Text = diary.Watch;
        EstabCadreCount.Text = IntToString(diary.EstabCadreCount);
        EstabSodierCount.Text = IntToString(diary.EstabSodierCount);
        EstabSum.Text = IntToString(diary.EstabCadreCount + diary.EstabSodierCount);
        ExistCadreCount.Text = IntToString(diary.ExistCadreCount);
        ExistSodierCount.Text = IntToString(diary.ExistSodierCount);
        ExistSum.Text = IntToString(diary.ExistCadreCount + diary.ExistSodierCount);
        Partition.Text = diary.Partition;
        Morn.Text = diary.Morn;
        MornDueCount.Text = IntToString(diary.MornDueCount);
        MornRealCount.Text = IntToString(diary.MornRealCount);

        if (!diary.MornDueCount.HasValue || diary.MornDueCount == 0)
        {
            MornQuotiety.Text = "";
        }
        else
        {
            decimal temp = ((decimal)diary.MornRealCount / (decimal)diary.MornDueCount) * 100;
            MornQuotiety.Text = temp.ToString("0.0") + "%";
        }


        Forenoon.Text = diary.Forenoon;
        ForenoonDueCount.Text = IntToString(diary.ForenoonDueCount);
        ForenoonRealCount.Text = IntToString(diary.ForenoonRealCount);
        if (!diary.ForenoonDueCount.HasValue || diary.ForenoonDueCount == 0)
        {
            ForenoonQuotiety.Text = "";
        }
        else
        {
            decimal temp = ((decimal)diary.ForenoonRealCount / (decimal)diary.ForenoonDueCount) * 100;

            ForenoonQuotiety.Text = temp.ToString("0.0") + "%";
        }
        Afternoon.Text = diary.Afternoon;
        AfternoonDueCount.Text = IntToString(diary.AfternoonDueCount);
        AfternoonRealCount.Text = IntToString(diary.AfternoonRealCount);
        if (!diary.AfternoonDueCount.HasValue || diary.AfternoonDueCount == 0)
        {
            AfternoonQuotiety.Text = "";
        }
        else
        {

            decimal temp = ((decimal)diary.AfternoonRealCount / (decimal)diary.AfternoonDueCount) * 100;
            AfternoonQuotiety.Text = temp.ToString("0.0") + "%";
        }


        Night.Text = diary.Night;
        NightDueCount.Text = IntToString(diary.NightDueCount);
        NightRealConnt.Text = IntToString(diary.NightRealConnt);


        if (!diary.NightDueCount.HasValue || diary.NightDueCount == 0)
        {
            NightQuotiety.Text = "";
        }
        else
        {

            decimal temp = ((decimal)diary.NightRealConnt / (decimal)diary.NightDueCount) * 100;
            NightQuotiety.Text = temp.ToString("0.0") + "%";
        }

        PublicWork.Text = diary.PublicWork;
        SoldierDevicesChange.Text = diary.SoldierDevicesChange;
        Ethos.Text = diary.Ethos;
        Others.Text = diary.Others;
        lblHeaderWord.Text = diary.HeaderWord;
        WordDate.Text = diary.WordDate.HasValue ? diary.WordDate.Value.ToString("HH时mm分") : "时&nbsp;&nbsp;&nbsp;&nbsp;分";
        string Departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (diary.HeaderWord == null && isOk(Departmentid,diary) && (diary.DiaryDate.Value.ToShortDateString() == DateTime.Now.ToShortDateString() || diary.DiaryDate.Value.ToShortDateString() == DateTime.Now.AddDays(-1).ToShortDateString()) || diary.DiaryDate.Value.ToShortDateString() == DateTime.Now.AddDays(-2).ToShortDateString())
        {
            Button1.Visible = true;
        }
        else
        {
            Button1.Visible = false;
        }


    }

    bool isOk(string departmentid, DiarySquadron Diary)
    {
        if (Diary.DepartmentId.Trim() == departmentid.Trim())
        {
            return true;
        }
        else
        {
            if (departmentid.Substring(4, 1) == "2")
            {
                string ShortName = Yipusoft.Account.BLL.Department.GetDepartment(departmentid).ShortName.Substring(0, 2);
                List<DepartmentInfo> departments = Yipusoft.Account.BLL.Department.GetList(" departmentid like '" + departmentid.Substring(0, 4) + "3__' and ShortName like '" + ShortName + "%'  ").ToList();
                if (departments.Count > 0)
                {
                    bool ok = false;
                    for (int i = 0; i < departments.Count; i++)
                    {
                        if (departments[i].DepartmentID.Trim() == Diary.DepartmentId.Trim())
                        {
                            ok = true;
                            break;
                        }
                    }
                    return ok;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    void BindDuty(Guid id)
    {


        DiaryDutyCircs duty = db.DiaryDutyCircs.Where(o => o.Did == id).FirstOrDefault();
        if (duty != null)
        {
            WatchHandOver.Text = duty.WatchHandOver;
            WatchSuccessor.Text = duty.WatchSuccessor;
            Constitutor.Text = duty.Constitutor;
            DutyCircs.Text = duty.Circs;
            DayHandOver.Text = duty.DayHandOver;
            DaySuccessor.Text = duty.DaySuccessor;
            KitchenHandOver.Text = duty.KitchenHandOver;
            KitchenSuccessor.Text = duty.KitchenSuccessor;
            DayDate.Text = duty.DayDate.Value.ToString("HH:mm");

            KitchenDate.Text = duty.KitchenDate.Value.ToString("HH:mm");
        }
        else
        {
            WatchHandOver.Text = "";
            WatchSuccessor.Text = "";
            Constitutor.Text = "";
            DutyCircs.Text = "";
            DayHandOver.Text = "";
            DaySuccessor.Text = "";
            KitchenHandOver.Text = "";
            KitchenSuccessor.Text = "";
            DayDate.Text = "";

            KitchenDate.Text = "";
        }


    }

    void BindFamily(Guid id)
    {
        List<DiaryFamily> familys = (from aa in db.DiaryFamily where aa.Did == id select aa).ToList();
        gvFamily.DataSource = familys;
        gvFamily.DataBind();
        if (familys.Count > 0)
        {
            SickCircs.Text = familys[0].SickCircs;
        }
        else
        {
            SickCircs.Text = "";
        }
    }

    void bindInspect(Guid id)
    {
        List<DiaryInspect> inspects = (from aa in db.DiaryInspect where aa.Did == id select aa).ToList();
        gvInspect.DataSource = inspects;
        gvInspect.DataBind();
    }

    void bindLeave(Guid id)
    {
        List<DiaryLeave> leaves = (from aa in db.DiaryLeave where aa.Did == id select aa).ToList();
        gvLeave.DataSource = leaves;
        gvLeave.DataBind();

    }

    string IntToString(int? i)
    {
        if (i.HasValue)
        {
            return i.Value.ToString();
        }
        else
        {
            return "";
        }
    }
    //签字
    protected void Button1_Click(object sender, EventArgs e)
    { 

        Guid id = new Guid(Request["id"]);
        DiarySquadron diary = db.DiarySquadron.Where<DiarySquadron>(o => o.Id == id).FirstOrDefault();
        diary.HeaderWord = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
        diary.WordDate = DateTime.Now;
        db.SubmitChanges();
        init();
    }
}
